package com.haitaoit.chengquan.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.haitaoit.chengquan.base.ExitApplication;

/**
 * 保存一些数据到手机本地文件夹工具类 PreferenceUtils
 * PreferenceUtils
 * <p/>
 * 刘霄鹏
 * <p/>
 * 2016年1月5日 上午11:11:52
 *
 * @version 1.0.0
 */
public class PreferenceUtils {
    /**
     * 保存在手机里面的文件名
     */
    private static final String FILE_NAME = "chengquan_date";

    public static String getPrefString(String key, final String defaultValue) {
        final SharedPreferences settings = ExitApplication.context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return settings.getString(key, defaultValue);
    }

    public static void setPrefString(final String key, final String value) {
        final SharedPreferences settings = ExitApplication.context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        settings.edit().putString(key, value).commit();
    }
    public static String getUserType(String key) {//key：account_type
        return ExitApplication.context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getString(key,"0");
    }
    public static boolean getPrefBoolean(final String key, final boolean defaultValue) {
        final SharedPreferences settings = ExitApplication.context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        return settings.getBoolean(key, defaultValue);
    }

    public static boolean hasKey(final String key) {
        return ExitApplication.context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
                .contains(key);
    }

    public static void setPrefBoolean(final String key, final boolean value) {
        final SharedPreferences settings = ExitApplication.context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        settings.edit().putBoolean(key, value).commit();
    }

    public static void setPrefInt(final String key, final int value) {
        final SharedPreferences settings = ExitApplication.context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        settings.edit().putInt(key, value).commit();
    }

    public static int getPrefInt(final String key, final int defaultValue) {
        final SharedPreferences settings = ExitApplication.context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        return settings.getInt(key, defaultValue);
    }

    public static void setPrefFloat(final String key, final float value) {
        final SharedPreferences settings = ExitApplication.context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        settings.edit().putFloat(key, value).commit();
    }

    public static float getPrefFloat(final String key, final float defaultValue) {
        final SharedPreferences settings = ExitApplication.context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        return settings.getFloat(key, defaultValue);
    }

    public static void setSettingLong(final String key, final long value) {
        final SharedPreferences settings = ExitApplication.context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        settings.edit().putLong(key, value).commit();
    }

    public static long getPrefLong(final String key, final long defaultValue) {
        final SharedPreferences settings = ExitApplication.context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        return settings.getLong(key, defaultValue);
    }

    public static void clearPreference(final SharedPreferences p) {
        final Editor editor = p.edit();
        editor.clear();
        editor.commit();
    }

    public static void setImgUrl(String url) {
        final SharedPreferences settings = ExitApplication.context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        settings.edit().putString("bannerImgUrl", url).commit();
    }
    public static String getImgUrl() {
        final SharedPreferences settings = ExitApplication.context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        return settings.getString("bannerImgUrl","");
    }
}
